package com.morefun.sm;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public final class LoginWindow {
    private Display display;
    private Shell shell;
    private Composite bottom;
    private Label name;
    private Label pass;
    private Text nameText;
    private Text passText;
    private Button ok;
    private Button exit;
    private Rectangle clientArea;
//    private RootHook rootHook;

    public LoginWindow(Object hook) {
//        rootHook = (RootHook) hook;
    }

    /**
     * 显示登陆界面
     */
    public void show() {
        Display.getDefault().dispose();
        display = new Display();
        clientArea = display.getClientArea();
        shell = new Shell(display, SWT.TITLE | SWT.MIN | SWT.MAX | SWT.CLOSE);
        FormLayout layout = new FormLayout();
        layout.marginHeight = 10;
        layout.marginWidth = 20;
        shell.setLayout(layout);
        shell.setText("用户登录");

        name = new Label(shell, SWT.NONE);
        name.setText("用户名");
        nameText = new Text(shell, SWT.SINGLE | SWT.BORDER);
        pass = new Label(shell, SWT.NONE);
        pass.setText("密码");
        passText = new Text(shell, SWT.SINGLE | SWT.BORDER);
        passText.setEchoChar('*');
        passText.setTabs(2);
        bottom = new Composite(shell, SWT.NONE);
        RowLayout rowLayout = new RowLayout();
        rowLayout.justify = true;
        bottom.setLayout(rowLayout);
        ok = new Button(bottom, SWT.PUSH);
        ok.setText("确定");
        ok.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                ok();
            }
        });
        exit = new Button(bottom, SWT.PUSH);
        exit.setText("取消");
        exit.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                cancel();
            }
        });

        FormData data = new FormData();
        data.top = new FormAttachment(15, 0);
        name.setLayoutData(data);

        data = new FormData();
        data.top = new FormAttachment(name, 0, SWT.CENTER);
        data.left = new FormAttachment(name, 10, SWT.RIGHT);
        nameText.setLayoutData(data);

        data = new FormData();
        data.top = new FormAttachment(name, 10, SWT.BOTTOM);
        pass.setLayoutData(data);

        data = new FormData();
        data.top = new FormAttachment(pass, 0, SWT.CENTER);
        data.left = new FormAttachment(nameText, 0, SWT.LEFT);
        passText.setLayoutData(data);

        data = new FormData();
        data.top = new FormAttachment(pass, 15, SWT.BOTTOM);
        data.left = new FormAttachment(pass, 0, SWT.LEFT);
        data.right = new FormAttachment(passText, 0, SWT.RIGHT);
        bottom.setLayoutData(data);

        shell.pack();
        Rectangle shellBounds = shell.getBounds();
        shell.setLocation(
            (clientArea.width - shellBounds.width) / 2,
            (clientArea.height - shellBounds.height) / 2);

        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
    }

    private void dispose() {
        display.dispose();
    }

    private void cancel() {
        dispose();
    }

    private void ok() {
        verify();
    }

     /**
	 * 功能描述 
	 */
	private void verify() {
		// TODO Auto-generated method stub
	}
	
	public static void main(String[] args) {
		new LoginWindow(null).show();
	}
}

